package com.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    static final String QUEUE_NAME = "simple_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //主机地址
        connectionFactory.setHost("localhost");
        //连接端口
        connectionFactory.setPort(5672);
        //虚拟主机名
        connectionFactory.setVirtualHost("/user_tastmo");
        //连接用户名
        connectionFactory.setUsername("user_tastmo");
        //连接密码
        connectionFactory.setPassword("123456");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //创建频道
        Channel channel = connection.createChannel();
        /**
         * 声明要创建的队列
         * 参数1 队列名称
         * 参数2 是否定义持久化队列
         * 参数3 是否独占本次连接
         * 参数4 是否在不适用的时候自动删除队列
         * 参数5 队列其他参数
         */
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        String message = "Hello World";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println("已发送消息" + message);

        channel.close();
        connection.close();
    }
}
